package com.bobi.tfca.controller.message;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bobi.tfca.model.constant.ResourcePathConstant;
import com.bobi.tfca.model.entity.MemberEntity;
import com.bobi.tfca.model.entity.RoleEntity;
import com.bobi.tfca.model.pojo.R;
import com.bobi.tfca.model.vo.MemberProfileVO;
import com.bobi.tfca.model.vo.MemberVO;
import com.bobi.tfca.model.vo.RoleVO;
import com.bobi.tfca.service.MemberManageService;
import com.bobi.tfca.service.MemberProfileService;
import com.bobi.tfca.service.MemberService;
import com.bobi.tfca.service.RoleService;
import com.bobi.tfca.utils.CosUtils;
import com.bobi.tfca.web.annotation.CurrentUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import retrofit2.http.Query;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 李文发
 * @date 2024/4/10
 */
@RestController
@RequestMapping("/member")
@Api(tags = "聊天室接口")
@Slf4j
public class MemberController {

    @Autowired
    private MemberService memberService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private MemberManageService memberManageService;

    @GetMapping("/getMemberById/{memberId}")
    @ApiOperation(value = "根据id查询用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "memberId", value = "用户id")
    })
    public R<MemberVO> getMemberById(@PathVariable("memberId") Integer memberId) {
        MemberVO memberVO = memberService.getMemberById(memberId);
        return memberVO != null ? R.data(memberVO) : R.failed("不存在该会员ID");
    }

    @GetMapping("/getMemberByAccount")
    @ApiOperation(value = "根据账号查询用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账号")
    })
    public R<String> getMemberByAccount(String account) {
        String email = memberService.getMemberByAccount(account);
        return email != null ? R.data(email) : R.failed("不存在账号");
    }

    @ApiOperation(value = "移除协会成员")
    @PostMapping("/removeAssMember/{memberId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "memberId", value = "用户id")
    })
    public R<String> removeAssMember(@PathVariable("memberId") Integer memberId) {
        Boolean success = memberService.removeAssMember(memberId);
        return R.success("移除成功");
    }

    @ApiOperation(value = "查询会员档案")
    @GetMapping("/getProfileByMemberId/{memberId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "memberId", value = "用户id")
    })
    public R<MemberProfileVO> getProfileByMemberId(@PathVariable("memberId") Integer memberId) {
        MemberProfileVO memberProfileVO = memberService.getProfileByMemberId(memberId);
        return R.data(memberProfileVO);
    }

    @ApiOperation(value = "查询在会成员")
    @GetMapping("/findAllStillMembers")
    public R<List<MemberVO>> findAllStillMembers() {
        List<MemberVO> memberVOList = memberService.findAllStillMembers();
        return R.data(memberVOList);
    }

    @ApiOperation(value = "查询离会成员")
    @GetMapping("/findAllLeaveMembers")
    public R<List<MemberVO>> findAllLeaveMembers() {
        List<MemberVO> memberVOList = memberService.findAllLeaveMembers();
        return R.data(memberVOList);
    }

    @ApiOperation(value = "根据名称查询退会会员")
    @GetMapping("/searchLeaveMemberByName")
    @ApiImplicitParams({@ApiImplicitParam(name = "searchContent", value = "姓名")})
    public R<List<MemberVO>> searchLeaveMemberByName(@RequestParam("searchContent") String searchContent) {
        List<MemberVO> memberVOList = memberService.searchLeaveMemberByName(searchContent);
        return R.data(memberVOList);
    }

    @ApiOperation(value = "根据名称查询在会会员")
    @GetMapping("/searchStillMemberByName")
    @ApiImplicitParams({@ApiImplicitParam(name = "searchContent", value = "姓名")})
    public R<List<MemberVO>> searchStillMemberByName(@RequestParam("searchContent") String searchContent) {
        List<MemberVO> memberVOList = memberService.searchStillMemberByName(searchContent);
        return R.data(memberVOList);
    }

    @ApiOperation(value = "修改密码")
    @PostMapping("/changePwByPw")
    @ApiImplicitParams({@ApiImplicitParam(name = "member", value = "当前用户"),
            @ApiImplicitParam(name = "oldPassword", value = "旧密码"),
            @ApiImplicitParam(name = "newPassword", value = "新密码")})
    public R<String> changePwByPw(@CurrentUser MemberEntity member, String oldPassword, String newPassword) {
        Boolean success = memberService.changePwByPw(member, oldPassword, newPassword);
        return success? R.success("修改密码成功") : R.failed("修改密码失败");
    }

    @ApiOperation(value = "找回密码")
    @PostMapping(value = "/resetPassword")
    @ApiImplicitParams({@ApiImplicitParam(name = "account", value = "账号"),
            @ApiImplicitParam(name = "code", value = "验证码"),
            @ApiImplicitParam(name = "newPassword", value = "新密码")})
    public R<String> resetPassword(String account, String newPassword, String code) {
        Integer result = memberService.resetPassword(account, newPassword, code);
        return switch (result) {
            case 0 -> R.success("重置密码成功");
            case 1 -> R.failed("验证码错误");
            case 2 -> R.failed("不存在此账号");
            default -> R.failed("找回失败");
        };
    }

    @ApiOperation(value = "注册会员")
    @PostMapping(value = "/registerMember")
    @ApiImplicitParams({@ApiImplicitParam(name = "memberProfileVO", value = "用户档案"),
            @ApiImplicitParam(name = "code", value = "验证码")})
    public R<String> registerMember(@RequestBody MemberProfileVO memberProfileVO, @RequestParam("code") String code) {
        Integer result = memberManageService.registerMember(memberProfileVO, code);
        return switch (result) {
            case 0 -> R.success("注册会员成功");
            case 1 -> R.failed("验证码错误");
            case 2 -> R.failed("手机号码已被注册");
            default -> R.failed("注册失败");
        };
    }

    @ApiOperation(value = "查询全部角色")
    @GetMapping("/getAllRoles")
    @ApiImplicitParams({@ApiImplicitParam(name = "member", value = "当前用户")})
    public R<List<RoleVO>> getAllRoles(@CurrentUser MemberEntity member) {
        MemberEntity entity = memberService.getById(member.getMemberId());
        List<RoleEntity> list = roleService.getLessLevelRole(entity.getRoleId());
        List<RoleVO> voList = list.stream().map(it -> BeanUtil.copyProperties(it, RoleVO.class)).toList();
        return R.data(voList);
    }

    @ApiOperation(value = "修改成员角色")
    @PostMapping("/changeMemberRole/{memberId}")
    @ApiImplicitParams({@ApiImplicitParam(name = "memberId", value = "用户id"),
            @ApiImplicitParam(name = "role", value = "角色")})
    public R<String> changeMemberRole(@PathVariable("memberId") Integer memberId, @RequestParam("role") String role) {
        Boolean success = memberService.changeMemberRole(memberId, role);
        return R.success("修改成功");
    }

    @ApiOperation(value = "查询成员菜单")
    @GetMapping("/getMemberMenus")
    @ApiImplicitParams({@ApiImplicitParam(name = "member", value = "当前用户")})
    public R<List<String>> getMemberMenus(@CurrentUser MemberEntity member) {
        List<String> list = memberService.getMemberMenus(member);
        if (list == null) {
            return R.failed("当前用户未登录");
        } else {
            return R.data(list);
        }
    }

    @ApiOperation(value = "登出")
    @PostMapping("/logout")
    @ApiImplicitParams({@ApiImplicitParam(name = "member", value = "当前用户")})
    public R<String> logout(@CurrentUser MemberEntity member) {
        memberService.logout(member);
        return R.success("登出成功");
    }

    @ApiOperation(value = "改变用户头像")
    @PostMapping("/updateMyHeadPhoto")
    @ApiImplicitParams({@ApiImplicitParam(name = "member", value = "当前用户"),
            @ApiImplicitParam(name = "file", value = "图片")})
    public R<String> updateMyHeadPhoto(@CurrentUser MemberEntity member, @RequestPart("file") MultipartFile file) {
        String url = memberService.updateMyHeadPhoto(member, file);
        return R.data(url);
    }
}
